package com.aptemo.webetek.client.widgets.betslip;

import com.aptemo.webetek.client.enums.EnumPickStatus;
import com.aptemo.webetek.client.events.DeletePickEvent;
import com.aptemo.webetek.client.events.DeletePickHandler;
import com.aptemo.webetek.client.events.HasDeletePickHandlers;
import com.aptemo.webetek.client.widgets.DateTimeWidget;
import com.aptemo.webetek.shared.PickBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class BetSlipRow
		extends Composite implements HasDeletePickHandlers
{

	private static BetSlipRowUiBinder	uiBinder	= GWT.create(BetSlipRowUiBinder.class);

	interface BetSlipRowUiBinder extends UiBinder<Widget, BetSlipRow>
	{
	}

	interface MyStyle extends CssResource
	{
		String winner();

		String looser();

	}

	@UiField
	MyStyle			style;

	@UiField
	Label			pickField;

	@UiField
	Label			betOddsField;

	@UiField
	Label			betDescriptionField;

	// @UiField
	// Label betLabelField;

	@UiField
	Label			betNameField;

	@UiField
	Label			betResultField;

	@UiField
	DateTimeWidget	betDateField;

	@UiField
	Anchor			removeBtn;

	@UiField
	Label			winnerPickField;

	@UiField
	HTMLPanel		resultPanel;

	@UiField
	Image			resultIconField;

	private String	id;

	public BetSlipRow(PickBean pick)
	{
		initWidget(uiBinder.createAndBindUi(this));

		this.id = pick.getId();

		// List<BetLabelBean> labels = pick.getBet().getLabels();
		// if (labels.size() > 0)
		// {
		// betLabelField.setText(labels.get(0).getName());
		// }
		// set date instead
		betDateField.setValue(pick.getEventDate());

		pickField.setText(pick.getTitle());

		betOddsField.setText("" + pick.getOdds());

		betNameField.setText(pick.getEventTitle());

		betDescriptionField.setText(pick.getEventDescription());

		// result?
		if ((pick.getStatus() != EnumPickStatus.NONE) && (pick.getEventWinPick() != null))
		{
			resultPanel.setVisible(true);

			winnerPickField.setText(pick.getEventWinPick().getTitle());
			betResultField.setText(pick.getEventResultDescription());

			if (pick.getStatus() == EnumPickStatus.WON)
			{
				resultIconField.setUrl("images/accept-icon.png");
			}
			else
			{
				resultIconField.setUrl("images/reject-icon.png");
			}
		}

		Element el = getElement();
		el.addClassName(pick.getStatus() == EnumPickStatus.WON ? style.winner() : style.looser());
		el.removeClassName(pick.getStatus() == EnumPickStatus.WON ? style.looser() : style.winner());
	}

	@UiHandler("removeBtn")
	void removePickHandler(ClickEvent event)
	{
		fireEvent(new DeletePickEvent(id));
	}

	@Override
	public HandlerRegistration addDeletePickHandler(DeletePickHandler handler)
	{
		return addHandler(handler, DeletePickEvent.TYPE);
	}

}
